package com.ruoyi.web.controller.module.market;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.market.domain.ConfigEmployee;
import com.ruoyi.market.service.IConfigEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 店铺店员配置信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/market/configEmployee")
public class ConfigEmployeeController extends BaseController
{
    @Autowired
    private IConfigEmployeeService configEmployeeService;

    /**
     * 获取店铺店员配置列表
     */
    @PostMapping("/configEmployeeList")
    public TableDataInfo list(@RequestBody ConfigEmployee configEmployee)
    {
        startPageByPost();
        List<ConfigEmployee> list = configEmployeeService.selectConfigEmployeeList(configEmployee);
        return getDataTable(list);
    }

    /**
     * 根据店铺店员配置编号获取详细信息
     */
    @PostMapping("/getConfigEmployee")
    public AjaxResult getConfigEmployee(@RequestBody Map<String, Object> params)
    {
        String shopId = params.get("shopId").toString(); // 店铺id
        if (shopId == null || shopId.equals("")) {
            return error("店铺店员配置失败，参数错误");
        }
        AjaxResult ajax = AjaxResult.success();
        ConfigEmployee sysConfigEmployee = configEmployeeService.selectConfigEmployeeByShopId(shopId);
        ajax.put(AjaxResult.DATA_TAG, sysConfigEmployee);
        return ajax;
    }

    /**
     * 新增店铺店员配置
     */
    @PreAuthorize("@ss.hasPermi('market:configEmployee:add')")
    @Log(title = "店铺店员配置管理 - 新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ConfigEmployee configEmployee)
    {
        configEmployee.setCreateBy(getUsername());
        return toAjax(configEmployeeService.insertConfigEmployee(configEmployee));
    }

    /**
     * 修改店铺店员配置
     */
    @PreAuthorize("@ss.hasPermi('market:configEmployee:edit')")
    @Log(title = "店铺店员配置管理 - 修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody ConfigEmployee configEmployee)
    {
        configEmployee.setUpdateBy(getUsername());
        return toAjax(configEmployeeService.updateConfigEmployee(configEmployee));
    }

    /**
     * 保存店铺店员配置
     */
    @PostMapping("/save")
    public AjaxResult save(@Validated @RequestBody ConfigEmployee configEmployee)
    {
        if(StringUtils.isNotNull(configEmployee) && StringUtils.isNotNull(configEmployee.getShopId())){
            ConfigEmployee cb = configEmployeeService.selectConfigEmployeeByShopId(configEmployee.getShopId());
            if(StringUtils.isNotNull(cb)){
                configEmployeeService.updateConfigEmployee(configEmployee);
            }else{
                configEmployeeService.insertConfigEmployee(configEmployee);
            }
        }else {
            return error("保存店铺店员配置失败，参数错误");
        }
        configEmployee.setCreateBy(getUsername());
        configEmployee.setUpdateBy(getUsername());
        return toAjax(configEmployeeService.updateConfigEmployee(configEmployee));
    }

    /**
     * 删除店铺店员配置
     */
    @PreAuthorize("@ss.hasPermi('market:configEmployee:remove')")
    @Log(title = "店铺店员配置管理 - 删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configEmployeeIds}")
    public AjaxResult remove(@PathVariable Long[] configEmployeeIds)
    {
        return toAjax(configEmployeeService.deleteConfigEmployeeByIds(configEmployeeIds));
    }
}
